import queue
import random
import threading
import time

import pymysql as pymysql
from fake_useragent import UserAgent
from lxml import etree
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


class footballFull():
    chrome_path = r'./chromedriver.exe'

    def __init__(self, url):
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        self.q = queue.Queue()
        self.q1 = queue.Queue()
        self.driver = webdriver.Chrome()
        self.base_url = url
        self.__host = '81.69.226.125'
        self.__port = 4510
        self.__user = 'root'
        self.__password = 'root'
        self.__db = 'titan007'
        self.__charset = 'utf8mb4'
        self.lists = []
        self.dicts = {}
        self.tr = []
        self.s = ""
        self.ua = UserAgent()
        self.total_tr = []

    # def sql_operate(self, lists):
    #     self.__conn = pymysql.connect(
    #         host=self.__host,
    #         port=self.__port,
    #         user=self.__user,
    #         password=self.__password,
    #         db=self.__db,
    #         charset=self.__charset
    #     )
    #     cursor = self.__conn.cursor()
    #     for single in lists:
    #         for key, value in single.items():
    #             if not value:
    #                 value = 'null'
    #                 single[key] = value
    #         sql_inert = "insert into tp_game values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    #         data = [(single['id'], single['game_type'], single['match_name'], single['match_date'], single['home_team'], \
    #                  single['away_team'], single['half_score'], single['final_score'], single['match_status'],
    #                  single['minutes'], \
    #                  single['fx_id'], single['hc'], single['hc_home'], single['hc_away'], single['ou'], single['ou_o'],
    #                  single['ou_u'], \
    #                  single['hc_half'], single['hc_home_half'], single['hc_away_half'], single['ou_half'],
    #                  single['ou_o_half'], \
    #                  single['ou_u_half'], single['style'], single['sort'], single['create_time'], single['update_time'],
    #                  single['tag'])]
    #         print(data)
    #         print('正在插入中')
    #         cursor.executemany(sql_inert, data)
    #         self.__conn.commit()
    #     cursor.close()
    #     self.__conn.close()

    def get_html(self):
        self.driver.get(self.base_url)
        self.driver.find_element(By.ID, 'button6').click()
        self.driver.find_element(By.XPATH, '//*[@id="tools"]/ul/li[1]').click()
        self.driver.implicitly_wait(10)
        complete_data = self.driver.page_source
        return complete_data

    # 解析出详情页url
    def parse_detail_url(self, complete_data):
        complete_html = etree.HTML(complete_data)
        total_tr = complete_html.xpath('//tbody/tr[@align="center"]')
        self.total_tr = total_tr
        suspend_id = complete_html.xpath('//*[@onmouseout="hiddenSbDetail()"]//@id')
        suspend_window = complete_html.xpath('//*[@id="sbOddsDetail"]/table/tbody/tr')
        window_all_data = complete_html.xpath('//*[@id="sbOddsDetail"]')
        return suspend_id, total_tr, suspend_window, window_all_data

    # 组装数据
    def assemble_data(self):
        for i in self.total_tr:
            sid = self.q.get()
            tr = self.q1.get()
            if tr.xpath('td[2]/a/font/text()'):
                match_name = "".join(tr.xpath('td[2]/a/font/text()'))
            else:
                match_name = "".join(tr.xpath('td[2]/text()'))

            match_date = "".join(tr.xpath('td[3]/text()'))
            minutes = "".join(tr.xpath('td[4]/text()'))
            home_team = "".join(tr.xpath('td[5]/a[3]/text()'))
            final_score = "".join(tr.xpath('td[6]/text()'))
            away_team = "".join(tr.xpath('td[7]/a[1]/text()'))
            half_score = "".join(tr.xpath('td[8]/span/text()'))
            undefined = "".join(tr.xpath('td[10]/div/text()'))
            undefined2 = "".join(tr.xpath('td[11]/div/text()'))
            undefined3 = "".join(tr.xpath('td[12]/div/text()'))
            create_id = time.time()
            # uid = str(create_id * 1000000)[0:-2:2]
            # rid = random.randint(1000, 9999)
            # uid = uid + str(rid)
            uid = random.randint(1, 99999999)
            times = str(create_id)[:10]
            self.dicts = {
                "id": uid,
                "game_type": 1,
                "match_name": match_name,
                "match_date": match_date,
                "home_team": home_team,
                "away_team": away_team,
                "half_score": half_score,
                "final_score": final_score,
                "match_status": 1,
                "minutes": minutes,
                "fx_id": 1,
                "hc": "",
                "hc_home": "0",
                "hc_away": "0",
                "ou": 0,
                "ou_o": "0",
                "ou_u": "0",
                "hc_half": "0",
                "hc_home_half": "0",
                "hc_away_half": "0",
                "ou_half": 0,
                "ou_o_half": "0",
                "ou_u_half": "0",
                "style": 0,
                "sort": 1,
                "create_time": times,
                "update_time": times,
                "tag": 0
            }

            if undefined2:
                self.s = sid
                window_all_data = self.get_window_selenium_contorl()
                for window_all_tbody in window_all_data:
                    cont = len(window_all_tbody.xpath('//*[@id="ffOddsDetail"]/tbody/tr[1]/td'))
                    if cont == 3:
                        # 让球，主队赔率
                        hc_home = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[2]/td[2]/div/text()')
                        if not hc_home:
                            hc_home = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[2]/td[2]/text()')
                        self.dicts["hc_home"] = "".join(hc_home)
                        # 让球
                        hc = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[2]/td[3]/div/text()')
                        if not hc:
                            hc = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[2]/td[3]/text()')
                        self.dicts["hc"] = "".join(hc)
                        # 让球，客队队赔率
                        hc_away = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[2]/td[4]/div/text()')
                        if not hc_away:
                            hc_away = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[2]/td[4]/text()')
                        self.dicts["hc_away"] = "".join(hc_away)

                        # 大小球，大球赔率
                        ou_o = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[4]/td[2]/div/text()')
                        if not hc_home:
                            ou_o = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[4]/td[2]/text()')
                        self.dicts["ou_o"] = "".join(ou_o)

                        # 大小球
                        ou = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[4]/td[3]/div/text()')
                        if not ou:
                            ou = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[4]/td[3]/text()')
                        self.dicts["ou"] = "".join(ou)

                        # 大小球，小球赔率
                        ou_u = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[4]/td[4]/div/text()')
                        if not ou_u:
                            ou_u = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[4]/td[4]/text()')
                        self.dicts["ou_u"] = "".join(ou_u)

                        # 半场让球，主队赔率【新增】
                        hc_home_half = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[2]/td[2]/div/text()')
                        if not hc_home_half:
                            hc_home_half = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[2]/td[2]/text()')
                        self.dicts["hc_home_half"] = "".join(hc_home_half)

                        # 半场让球【新增】
                        hc_half = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[2]/td[3]/div/text()')
                        if not hc_half:
                            hc_half = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[2]/td[3]/text()')
                        self.dicts["hc_half"] = "".join(hc_half)

                        # 半场让球【新增】
                        hc_away_half = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[2]/td[4]/div/text()')
                        if not hc_away_half:
                            hc_away_half = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[2]/td[4]/text()')
                        self.dicts["hc_away_half"] = "".join(hc_away_half)

                        # 半场大小球，大球赔率【新增】
                        ou_o_half = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[4]/td[2]/div/text()')
                        if not ou_o_half:
                            ou_o_half = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[4]/td[2]/text()')
                        self.dicts["ou_o_half"] = "".join(ou_o_half)

                        # 半场大小球【新增】
                        ou_half = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[4]/td[3]/div/text()')
                        if not ou_half:
                            ou_half = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[4]/td[3]/text()')
                        self.dicts["ou_half"] = "".join(ou_half)

                        # 半场大小球，小球赔率【新增】
                        ou_u_half = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[4]/td[4]/div/text()')
                        if not ou_u_half:
                            ou_u_half = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[4]/td[4]/text()')
                        self.dicts["ou_u_half"] = "".join(ou_u_half)
                        # print(match_name, hc_home, hc, hc_away, ou_o,
                        #       ou, ou_u, hc_home_half, hc_half, hc_away_half, ou_o_half,
                        #       ou_half, ou_u_half)
                    if cont == 4:
                        # 让球，主队赔率
                        hc_home = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[2]/td[5]/div/text()')
                        if not hc_home:
                            hc_home = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[2]/td[5]/text()')
                        self.dicts["hc_home"] = "".join(hc_home)
                        # 让球
                        hc = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[2]/td[6]/div/text()')
                        if not hc:
                            hc = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[2]/td[6]/text()')
                        self.dicts["hc"] = "".join(hc)

                        # 让球，客队队赔率
                        hc_away = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[2]/td[7]/div/text()')
                        if not hc_away:
                            hc_away = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[2]/td[7]/text()')
                        self.dicts["hc_away"] = "".join(hc_away)

                        # 大小球，大球赔率
                        ou_o = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[4]/td[5]/div/text()')
                        if not ou_o:
                            ou_o = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[4]/td[5]/text()')
                        self.dicts["ou_o"] = "".join(ou_o)

                        # 大小球
                        ou = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[4]/td[6]/div/text()')
                        if not ou:
                            ou = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[4]/td[6]/text()')
                        self.dicts["ou"] = "".join(ou)

                        # 大小球，小球赔率
                        ou_u = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[4]/td[7]/div/text()')
                        if not ou_u:
                            ou_u = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[2]/tbody/tr[4]/td[7]/text()')
                        self.dicts["ou_u"] = "".join(ou_u)

                        # 半场让球，主队赔率【新增】
                        hc_home_half = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[2]/td[5]/div/text()')
                        if not hc_home_half:
                            hc_home_half = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[2]/td[5]/text()')
                        self.dicts["hc_home_half"] = "".join(hc_home_half)

                        # 半场让球【新增】
                        hc_half = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[2]/td[6]/div/text()')
                        if not hc_half:
                            hc_half = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[2]/td[6]/text()')
                        self.dicts["hc_half"] = "".join(hc_half)

                        # 半场让球【新增】
                        hc_away_half = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[2]/td[7]/div/text()')
                        if not hc_away_half:
                            hc_away_half = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[7]/td[4]/text()')
                        self.dicts["hc_away_half"] = "".join(hc_away_half)

                        # 半场大小球，大球赔率【新增】
                        ou_o_half = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[4]/td[2]/div/text()')
                        if not ou_o_half:
                            ou_o_half = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[4]/td[2]/text()')
                        self.dicts["ou_o_half"] = "".join(ou_o_half)

                        # 半场大小球【新增】
                        ou_half = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[4]/td[6]/div/text()')
                        if not ou_half:
                            ou_half = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[4]/td[6]/text()')
                        self.dicts["ou_half"] = "".join(ou_half)

                        # 半场大小球，小球赔率【新增】
                        ou_u_half = window_all_tbody.xpath(
                            '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[4]/td[7]/div/text()')
                        if not ou_u_half:
                            ou_u_half = window_all_tbody.xpath(
                                '//*[@id="sbOddsDetail"]/table[4]/tbody/tr[4]/td[7]/text()')
                        self.dicts["ou_u_half"] = "".join(ou_u_half)
                        # print(match_name, hc_home, hc, hc_away, ou_o,
                        #       ou, ou_u, hc_home_half, hc_half, hc_away_half, ou_o_half,
                        #       ou_half, ou_u_half)

            else:
                self.dicts["hc_home"] = ""
                self.dicts["hc"] = ""
                self.dicts["hc_away"] = ""
                self.dicts["ou_o"] = ""
                self.dicts["ou_u"] = ""
                self.dicts["hc_home_half"] = ""
                self.dicts["hc_half"] = ""
                self.dicts["hc_away_half"] = ""
                self.dicts["ou_o_half"] = ""
                self.dicts["ou_half"] = ""
                self.dicts["ou_u_half"] = ""
            self.lists.append(self.dicts)
            print(self.dicts)
        return self.lists

    # selenium浮窗数据
    def get_window_selenium_contorl(self):
        # print("self.s ", self.s)]
        time.sleep(2)
        s_id = self.driver.find_element(By.ID, self.s)
        ActionChains(self.driver).move_to_element(s_id).perform()
        self.driver.implicitly_wait(10)
        complete_data = self.driver.page_source
        complete_html = etree.HTML(complete_data)
        window_all_data = complete_html.xpath('//*[@id="sbOddsDetail"]')
        return window_all_data

    def main(self):
        complete_data = self.get_html()
        suspend_id, total_tr, suspend_window, window_all_data = self.parse_detail_url(complete_data)
        self.tr = total_tr
        for sid in suspend_id:
            self.q.put(sid)
        for tr in total_tr:
            self.q1.put(tr)
        for x in range(200):
            t = threading.Thread(target=self.assemble_data)
            t.start()


if __name__ == '__main__':
    run = footballFull('http://live.titan007.com/oldIndexall.aspx')
    run.main()
